package com.example.admin.service.Impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.BlogMapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.BlogService;

import com.example.xo.pojo.Blog;
import com.example.xo.pojo.Category;
import com.example.xo.xo.BlogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 992c
 */
@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Blog blog) {
        //雪花id
        String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
        //获取title封装为新的对象
        Blog byTitle = findByBlogTitle(blog.getTitle());
        //判断不为null代表有重复的标题，返回错误
        if (byTitle != null) {
            return false;
        }
        //返回变动的行数为一，因为这里只增加了一行
        return blogMapper.insert(blog) == 1;
    }

    @Override
    public List<BlogVo> findAll() {
        //创建一个集合
        List<BlogVo> oc = new ArrayList<>();
        //创建一个用来收集条件的容器
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        //判断是删除时间是null的时候要
        wrapper.isNull(Blog::getDeleted);
        //判断根据创建时间进行倒序排序
        wrapper.orderByDesc(Blog::getCreated);
        //将获取的数字封装成集合
        List<Blog> blogs = blogMapper.selectList(wrapper);
        //遍历集合
        for (Blog blog : blogs) {
            //将获取的id封装成对象
            Category category = categoryMapper.selectById(blog.getCategoryId());
            //new一个vo的对象，因为查询的数据有些我们用不到所以采用新建一个类来装我们需要的数据
            BlogVo vo = new BlogVo();

            vo.setBlogId(blog.getBlogId());
            vo.setCategoryName(category.getCategoryName());
            vo.setAuthor(blog.getAuthor());
            vo.setTitle(blog.getTitle());
            vo.setCreated(blog.getCreated());
            //将获取的数据使用add的方法加入集合，并返回
            oc.add(vo);
        }
        return oc;
    }

    @Override
    public BlogVo findById(String id) {
        //创建一个条件对比的容器
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
//        wrapper.orderByDesc(Blog::getCreated);

        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId, id);
//        Category category = categoryMapper.selectById(wrapper);
        //将根据条件获取的数据封装为对象
        Blog blog = blogMapper.selectOne(wrapper);
        //判断是不是空
        if (blog == null) {
            //如果是就直接返回null
            return null;
        }

        return this.meod(blog);
    }

    @Override
    public Blog findBlogById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, id);
        wrapper.isNull(Blog::getDeleted);
        Blog blog = blogMapper.selectOne(wrapper);
        return blog;
    }

    @Override
    public BlogVo findByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Blog::getCreated);
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);
//        Category category = categoryMapper.selectById(wrapper);
        Blog blog = blogMapper.selectOne(wrapper);
        return this.meod(blog);
    }

    @Override
    public boolean deleteById(String id) {

        //获取findBlogById方法的id
        Blog blog = findBlogById(id);
        //判断blog中的id是否为null
        if (blog == null) {
            //返回错误
            return false;
        }
        //创建删除时间
        blog.setDeleted(LocalDateTime.now());
        //创建一个容器
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        //对比条件获取的id和输入的id进行对比
        wrapper.eq(Blog::getBlogId, id);
        //获取改变的行数
        int update = blogMapper.update(blog, wrapper);
        //如果返回的是一就代表软删除成功，是零就代表软删除错误
        return update == 1;
    }

    @Override
    public boolean update(Blog blog) {

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        //将获取的id与返回值里的id进行对比
        wrapper.eq(Blog::getBlogId,blog.getBlogId());
        wrapper.isNull(Blog::getDeleted);
        //创建修改时间
        blog.setUpdated(LocalDateTime.now());
        //获取标题
        BlogVo byTitle = findByTitle(blog.getTitle());
        //如果标题不为空 并且 返回值里的id不等于标题的id
        if (byTitle != null && !blog.getBlogId().equals(byTitle.getBlogId())) {
            //返回false
            return false;
        }
        //将作者的修改屏蔽掉
        blog.setAuthor(null);

        int update = blogMapper.update(blog, wrapper);
        //只要有改动就会大于零
        return update > 0;
    }

    @Override
    public Blog findPoById(String id) {
        LambdaQueryWrapper<Blog>wrapper=new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        Blog blog = blogMapper.selectOne(wrapper);
        return blog;
    }

    /**
     * 重复使用
     *
     * @param blog
     * @return
     */
    private BlogVo meod(Blog blog) {

        if (blog == null) {
            return null;
        }

        Category category = categoryMapper.selectById(blog.getCategoryId());
        BlogVo vo = new BlogVo();
        vo.setBlogId(blog.getBlogId());
        vo.setCategoryName(category.getCategoryName());
        vo.setAuthor(blog.getAuthor());
        vo.setTitle(blog.getTitle());
        vo.setCreated(blog.getCreated());
        return vo;
    }

    public Blog findByBlogTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Blog::getCreated);
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getTitle, title);

        Blog blog = blogMapper.selectOne(wrapper);
        return blog;
    }
}
